package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn;

import static com.example.tnot.login.fragmentui.LoginFragment.BASE_URL;
import static com.example.tnot.login.fragmentui.LoginFragment.teacher_data;

import android.os.Bundle;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn.Adapter.rv_ClockInAdapter;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn.ItemBean.ClockIn;
import com.example.tnot.api.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class ClockInFragment extends BaseBackFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private rv_ClockInAdapter rv_clockInAdapter;
    private String mParam1;
    private String mParam2;
    private List<ClockIn> data;
    View view;
    ImageView back;
    LinearLayout ll_dropdown;
    RecyclerView rv_lockin;

    public ClockInFragment() {
    }

    public static ClockInFragment newInstance(String param1, String param2) {
        ClockInFragment fragment = new ClockInFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_clock_in_fragment, container, false);
        try {
            InitData();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
//        ll_dropdown = view.findViewById(R.id.tv_dropdown);
//        ll_dropdown.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//        ll_dropdown.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        data = new ArrayList<>();
        rv_lockin = view.findViewById(R.id.rv_lockin);
        rv_clockInAdapter=new rv_ClockInAdapter(data);
        rv_lockin.setAdapter(rv_clockInAdapter);

        rv_lockin.setLayoutManager(new LinearLayoutManager(getActivity()));

        ll_dropdown = view.findViewById(R.id.tv_dropdown);
//        ll_dropdown.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                for (int i = 0; i < data.size(); i++) {
//                    ClockIn clockIn = data.get(i);
//                    clockIn.setExpanded(!clockIn.isExpanded());
//                }
//                rv_lockin.getAdapter().notifyDataSetChanged();
//            }
//        });

        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });
        return view;
    }



    private void InitData() throws JSONException {

        String URL = BASE_URL+"api/get_checkin";
        HashMap<String, String> params = new HashMap<>();

        //这里需要课程的id

        params.put("course_id", "1");


        String jsonData = HttpUtils.buildJsonString(params);

        HttpUtils.sendPostRequest(URL, jsonData, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String response) throws JSONException {

                Log.d("签到信息", "网络请求成功，响应数据：" + response);
                InitData2(response); // 解析并添加数据
                // 主线程刷新适配器
                refreshTeachAdapter();
            }

            @Override
            public void onFailure(String errorMsg) {
                JSONArray datas=null;
                //头像，姓名，签到次数(默认为null)，总签到次数(默认为null)，各个签到类型签到次数
                data.add(new ClockIn(R.drawable.example_face, "小张", "null", "null", Arrays.asList(5,0,1,1,1),datas));
                data.add(new ClockIn(R.drawable.example_face, "晓东", "null", "null",Arrays.asList(4,2,2,2,2),datas));
                data.add(new ClockIn(R.drawable.example_face, "小猫", "null", "null", Arrays.asList(6,3,3,3,3),datas));

                refreshTeachAdapter();
            }
        });


        refreshTeachAdapter();
    }

    private void InitData2(String response) throws JSONException {
        JSONArray check_list_data1= new JSONArray(response);
        JSONArray check_list_data = check_list_data1.getJSONObject(2).getJSONArray("data");
        Log.d("签到信息",check_list_data.toString());
        for(int i=0;i<check_list_data.length();i++)
        {
            JSONObject check_data= check_list_data.getJSONObject(i);


            JSONArray checkIn_list =check_data.getJSONArray("checkin_list");
            JSONArray date_list=check_data.getJSONArray("times");
            Log.d("签到信息切割",checkIn_list.toString());
            ArrayList<Integer> checkin_list = new ArrayList<>();
            for(int j=0;j<checkIn_list.length();j++)
            {
                checkin_list.add(checkIn_list.getInt(j));
            }

            data.add(new ClockIn(check_data.getInt("student_id"),
                    check_data.getString("student_name"),
                    "null",
                    "null",
                    checkin_list,
                    date_list
                    //Arrays.asList(5,0,1,1,1)

            ));
        }

    }

    private void refreshTeachAdapter() {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (rv_clockInAdapter != null) {
                        rv_clockInAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }
}